#include "CallsHandle.h"

#include "PKB.h"

CallsHandle::CallsHandle()
{
}

CallsHandle::~CallsHandle()
{
}

vector<vector<INTEGER>> CallsHandle::handleCalls(vector<PROCINDEX> leftList, vector<PROCINDEX> rightList)
{
	vector<vector<INTEGER>> temp;
	vector<INTEGER> a;
	vector<INTEGER> b;
	temp.push_back(a);
	temp.push_back(b);
	//put right list into set for filtering
	set<INTEGER> rightListSet;
	for (vector<INTEGER>::iterator rightListIterator=rightList.begin();rightListIterator!=rightList.end();rightListIterator++)
	{
		rightListSet.insert((*rightListIterator));
	}
	//generator list of called functions from the leftList
	for (int i=0;i<leftList.size();i++)
	{
		set<INTEGER> tempResultOfOneProc = PKB::calls.GetCallee(leftList[i]);
		if (tempResultOfOneProc.size()>0)
		{
			for (set<INTEGER>::iterator oneProcResultItr = tempResultOfOneProc.begin(); oneProcResultItr != tempResultOfOneProc.end(); oneProcResultItr++)
			{
				if (rightListSet.find((*oneProcResultItr))!=rightListSet.end())
				{
					temp[0].push_back(leftList[i]);
					temp[1].push_back((*oneProcResultItr));
				}
			}
		}
	}

	
	if ((temp[0].size()==0)&&(temp[1].size()==0))
	{
		vector<vector<INTEGER>> result;
		return result;
	}
	else
		return temp;
};

vector<vector<INTEGER>> CallsHandle::handleCallsT(vector<PROCINDEX> leftList,vector<PROCINDEX> rightList)
{
	vector<vector<INTEGER>> temp;
	vector<INTEGER> a;
	vector<INTEGER> b;
	temp.push_back(a);
	temp.push_back(b);
	//put right list into set for filtering
	set<INTEGER> rightListSet;
	for (vector<INTEGER>::iterator rightListIterator=rightList.begin();rightListIterator!=rightList.end();rightListIterator++)
	{
		rightListSet.insert((*rightListIterator));
	}
	//generator list of called functions from the leftList
	for (int i=0;i<leftList.size();i++)
	{
		set<INTEGER> tempResultOfOneProc = PKB::calls.RetrieveTransitiveCallee(leftList[i]);
		if (tempResultOfOneProc.size()>0)
		{
			for (set<INTEGER>::iterator oneProcResultItr = tempResultOfOneProc.begin(); oneProcResultItr != tempResultOfOneProc.end(); oneProcResultItr++)
			{
				if (rightListSet.find((*oneProcResultItr))!=rightListSet.end())
				{
					temp[0].push_back(leftList[i]);
					temp[1].push_back((*oneProcResultItr));
				}
			}
		}
	}

	
	if ((temp[0].size()==0)&&(temp[1].size()==0))
	{
		vector<vector<INTEGER>> result;
		return result;
	}
	else
		return temp;
}